--
-- Author: shunguo.chen
-- Date: 2017-01-09 16:20:20
--

local GameDB = import(".GameDB")
local FileUtils = cc.FileUtils:getInstance()

function GameDB:getImagePath(root,iconName)
    if not root or not iconName then return nil end
    local iconPath = root .. iconName .. ".png"
    local fullPath = FileUtils:fullPathForFilename(iconPath)
    if not FileUtils:isFileExist(fullPath) then
        iconPath = root .. iconName .. ".jpg"
        fullPath = FileUtils:fullPathForFilename(iconPath)
    end

    if not FileUtils:isFileExist(fullPath) then
        return nil
    end
    return fullPath
end

function GameDB:isImagePathExsit(path)
    local fullPath = FileUtils:fullPathForFilename(path)
    local fileExist = FileUtils:isFileExist(fullPath)
    if fileExist then
        return fullPath
    end
    return nil
end


--种族头像
function GameDB:getRaceIconPath(raceId)
    local iconPath = Res.RaceIcons[tonumber(raceId)]
    return iconPath
end

--职业
function GameDB:getClassIconPath(classId,index)
    local classConfig = self:queryClass(classId)
    if not classConfig then return nil end
    local iconPath = nil
    if index == 1 then
        if not classConfig.icon then return nil end
        iconPath = self:getImagePath(Res.classIconPath,classConfig.icon)
    elseif index == 2 then
        if not classConfig.icon2 then return nil end
        iconPath = self:getImagePath(Res.classIconPath,classConfig.icon2)
    elseif index == 3 then
        if not classConfig.icon3 then return nil end
        iconPath = self:getImagePath(Res.classIconPath,classConfig.icon3)
    elseif index == 4 then
        if not classConfig.icon4 then return nil end
        iconPath = self:getImagePath(Res.classIconPath,classConfig.icon4)
    elseif index == 5 then
        if not classConfig.icon5 then return nil end
        iconPath = self:getImagePath(Res.classIconPath,classConfig.icon5)
    elseif index == 6 then
        if not classConfig.icon6 then return nil end
        iconPath = self:getImagePath(Res.classIconPath,classConfig.icon6)
    end
    return iconPath
end


--坐骑头像
function GameDB:getMountIconPath(mountId)
    local mountResConfig = self:getMountResConfig(mountId)
    if not mountResConfig then
        return nil
    end
    if not mountResConfig.icon then
        return nil
    end

    return self:getImagePath(Res.mountIconPath,mountResConfig.icon)
end

--佣兵
function GameDB:getMercenaryRaceIconPath(raceId)
    local mercRaceConfig = self:queryMercenaryRaceById(raceId)
    if not mercRaceConfig or not mercRaceConfig.icon then return nil end
    local raceIconPath = self:getImagePath(Res.mercRaceIconPath,mercRaceConfig.icon)

    return raceIconPath
end


--道具

--货币
function GameDB:getResIcon(resType)
    local iconPath = nil
    local rt=resType or Enums.PropType.gold
    local coinConfig = self:getCoinConfig(rt)
    if coinConfig and coinConfig.ID then
        iconPath = self:getImagePath(Res.coinIconPath,coinConfig.icon)
    end
    return iconPath
end

function GameDB:getCoinIconPath(icon)
    if not icon then return nil end

    local path = self:getImagePath(Res.coinIconPath,icon)
    return path
end

function GameDB:getToolIconPath(toolId)
    if not toolId then return nil end
    local toolResConfig = self:getToolResConfig(toolId)
    if not toolResConfig then
        return nil
    end
    if not toolResConfig.icon then
        return nil
    end

    local toolPath = self:getImagePath(Res.itemPath,toolResConfig.icon)
    return toolPath
end

function GameDB:getToolIconBgPath(toolId)
    if not toolId then return nil end
    local toolDB =self:getToolConfig(toolId)
    return Res.Qulity_Icon_Bg[tonumber(toolDB.quality)]
end

-- 检测道具掉落是否包含赠送钻石
-- drop里面掉落包含 "94|0|" 则认为有赠送钻石
function GameDB:checkItemHasPresentCoin(tid)
    local conf = self:queryItemById(tid)
    local drop = conf and conf.drop
    if drop then
        local dropConf = self:queryDrop(drop)
        local dropGroup = dropConf and dropConf.dropGroup
        if dropGroup then
            return string.isContain(dropGroup, "94|0|")
        end
    end
    return false
end

function GameDB:getItemIconPath(itemId)
    if not itemId then return nil end
    local itemConf = self:queryItemById(itemId)
    if not itemConf then
        return nil
    end
    if not itemConf.icon then
        return nil
    end

    local itemPath = self:getImagePath(Res.itemPath,itemConf.icon)
    return itemPath
end

function GameDB:getItemIconPathByType(itemType,itemId)
    local iconPath = nil
    local iconBgPath = nil
    local iconTypePath = nil
    local tipsType = Enums.TipsType.item
    if itemType == Enums.PropType.itemId then--道具
        tipsType = Enums.TipsType.item
        iconPath = self:getItemIconFile(Enums.ItemBagType.item,itemId)
        iconBgPath = self:getItemIconBgFile("item",itemId)
    elseif itemType == Enums.PropType.weaponId then--装备
        tipsType = Enums.TipsType.equip
        iconPath = self:getItemIconFile(Enums.ItemBagType.equip,itemId)
        iconBgPath = self:getItemIconBgFile("equip",itemId)
    elseif itemType == Enums.PropType.mountId then--坐骑
        tipsType = Enums.TipsType.mount
        iconPath = self:getItemIconFile(Enums.ItemBagType.mount,itemId)
        iconBgPath = self:getItemIconBgFile("mount",itemId)
    elseif itemType == Enums.PropType.jewel then
        tipsType = Enums.TipsType.jewel
        local db = self:queryJewelById(itemId)
        iconPath,iconBgPath = self:getJewelIconImgs(db.icon,db.quality)
    elseif itemType == Enums.PropType.jewelChip then
        local jewelChipConfig = self:getCoinConfig(Enums.PropType.jewelChip)
        iconPath = self:formatIconFilePath(Enums.ItemBagType.daibi, jewelChipConfig.icon)
    elseif itemType == Enums.PropType.toolId then
        tipsType = Enums.TipsType.tool
        iconPath = self:getItemIconFile(Enums.ItemBagType.tool,itemId)
        iconBgPath = self:getItemIconBgFile("tool",itemId)
    elseif itemType == Enums.PropType.mercenary then
        local partnerConfig = self:queryPartnerById(itemId)
        if partnerConfig and partnerConfig.icon then
            iconPath = self:getImagePath(Res.partnerIconPath,partnerConfig.icon)
        end
    elseif itemType == 100 then
        tipsType = nil
        local icon = GD:queryFameById(itemId).icon
        iconPath = GD:formatFameLevelIconFilePath(icon)
    elseif itemType > 90 then
        tipsType = Enums.TipsType.daibi
        iconPath = self:getItemIconFile(Enums.ItemBagType.daibi,itemType)
    end
    iconBgPath = iconBgPath or Res.Qulity_Icon_Bg[1]
    return tipsType,iconPath,iconBgPath,iconTypePath
end


function GameDB:getItemIconBgFile(type,tid)
    local iconBgName = nil
    local qulity = nil
    if type == "item" then
        local config = self:queryItemById(tid) or {}
        qulity = config.quality
    elseif type == "weapon" or type == "equip" then
        local config = self:getEquipConf(tid) or {}
        qulity = config.quality
    elseif type == "mask" then
        --TODO
    elseif type == "mount" then
        local config = self:getMountConfig(tid) or {}
        qulity = config.type
    elseif type == "fashion" then
        -- TODO
    elseif type == "merc" then
        -- TODO
    elseif type == "tool" then
        local config = self:getToolConfig(tid) or {}
        qulity = config.quality
    end

    if qulity then
        iconBgName = Res.Qulity_Icon_Bg[tonumber(qulity)]
    end

    return iconBgName
end


function GameDB:getItemIconBgPath(itemId)
    if not itemId then return nil end
    local itemDB =self:queryItemById(itemId)
    return Res.Qulity_Icon_Bg[tonumber(itemDB.quality)]
end

function GameDB:getItemIconBgByType(itemType,itemId)
    local iconBgPath = nil
    if itemType == 1 then--道具
        iconBgPath = self:getItemIconBgFile("item",itemId)
    elseif itemType == 2 then--装备
        iconBgPath = self:getItemIconBgFile("equip",itemId)
    elseif itemType == 4 then--坐骑
        iconBgPath = self:getItemIconBgFile("mount",itemId)
    elseif itemType >= 90 then
    end
    return iconBgPath
end

-------------------------------成就-----------------------------------
function GameDB:getAchievementIconPath(achId)
    local achievementConfig = self:getAchievementConfig(achId)
    if not achievementConfig or not achievementConfig.icon then
        return nil
    end
    local iconPath = self:getImagePath(Res.achievementIconPath,achievementConfig.icon)
    return iconPath
end



-------------------------------活动-----------------------------------
function GameDB:getActivityIconPathById(id)
    if not id then return nil end
    local activityConfig = ZD:queryActivityById(id)
    if activityConfig and activityConfig.icon then
        local iconPath = self:getImagePath(Res.activityIconPath,activityConfig.icon)
        return iconPath
    end
    return nil
end

function GameDB:getActivityBannerIconPathById(activityId)
    if not activityId then return nil end
    local activityConfig = ZD:queryActivityById(activityId)
    if activityConfig and activityConfig.bannerPath then
        local iconPath = self:getImagePath(Res.activityIconPath,activityConfig.bannerPath)
        return iconPath
    end
    return nil
end

--装备

function GameDB:getWeaponIconPath(weaponId)
    local weaponResConfig = self:getEquipResConf(weaponId)
    if not weaponResConfig then
        return nil
    end
    if not weaponResConfig.icon then
        return nil
    end
    local iconPath = self:getImagePath(Res.weaponResPath,weaponResConfig.icon)

    return iconPath
end

function GameDB:getEquipIconBgPath(itemId)
    if not itemId then return nil end
    local itemDB =self:getEquipConf(itemId) or {}
    return Res.Qulity_Icon_Bg[tonumber(itemDB.quality)]
end

--配方
function GameDB:getFormulaIconPath(formulaId)
    local formulaConfig = self:getFormulaConfig(formulaId)
    if not formulaConfig then
        return nil
    end
    if not formulaConfig.icon then
        return nil
    end

    local itemPath = self:getImagePath(Res.itemPath,formulaConfig.icon)

    return itemPath
end

--技能
function GameDB:getSkillIconPathByName(icon)
    if icon then
        return self:getImagePath(Res.skillIconPath,icon)
    end
    return nil
end

function GameDB:getSkillIconPath(skillId)
    local skillConfig = self:getSkillConfig(skillId)
    if not skillConfig then
        return nil
    end
    if not skillConfig.icon then
        return nil
    end

    local skillIconPath = self:getImagePath(Res.skillIconPath,skillConfig.icon)

    return skillIconPath
end

function GameDB:getClassSkillIconPath(class_skillId)
    local classSkillConfig = self:getClassSkill(class_skillId)
    if not classSkillConfig then
        return nil
    end
    if not classSkillConfig.icon then
        return nil
    end

    local skillIconPath = self:getImagePath(Res.skillIconPath,classSkillConfig.icon)

    return skillIconPath
end

--Buff
function GameDB:getBuffIconPath(buffId)
    local buffConfig = self:getBuffConfig(buffId)
    if not buffConfig then
        return nil
    end
    if not buffConfig.icon then
        return nil
    end

    local buffIconPath = self:getImagePath(Res.buffIconPath,buffConfig.icon)

    return buffIconPath
end

--珠宝
function GameDB:getJewelIconImgs(icon,quality)
    local iconImg=""
    local iconBg=""
    if icon then
        iconImg = self:formatIconFilePath(Enums.ItemBagType.jewel,icon)
    end
    if quality then
        iconBg = Res.Qulity_Icon_Bg[tonumber(quality)]
    else
        iconBg = Res.Qulity_Icon_Bg[1]
    end
    return iconImg,iconBg
end

-- 获得单个引导配置
function GameDB:getGuideConf(id)
    return self:getCachedValue("guide", id)
end

-- 获得所有引导配置
function GameDB:getGuideConfs()
    return self:getCachedValues("guide")
end

-- 获得所有引导配置
function GameDB:getBattleGuideConfs()
    return self:getCachedValues("teachbattle")
end

-- 获得所有引导配置
function GameDB:getBattleGuideConf(id)
    return self:getCachedValue("teachbattle", id)
end

function GameDB:getFeatureOnMenuIconPath(featureId)
    local featureOnConfig = self:queryFeatureOnById(featureId)
    local icon1Path = nil
    local icon2Path = nil
    if featureOnConfig and featureOnConfig.menuNormalIcon and featureOnConfig.menuDownIcon then
        icon1Path = Res.menuIconPath..featureOnConfig.menuNormalIcon..".png"
        icon2Path = Res.menuIconPath..featureOnConfig.menuDownIcon..".png"
    end
    return icon1Path,icon2Path
end

-- 创建角色所需数据
function GameDB:getCreateDatas()
    if not self._createDatas then
        local ret = {}
        local confs = self:getCachedTable("create_total")
        for k,v in pairs(confs) do
            local classID = tonumber(v.classID)
            local raceID = tonumber(v.raceID)
            local gender = tonumber(v.gender)

            local vo = {}
            vo.classID = classID
            vo.raceID = raceID
            vo.gender = gender
            vo.faceIDs = table.tonumber(string.split(v.faceIDs, ";"))
            vo.hairIDs = table.tonumber(string.split(v.hairIDs, ";"))
            vo.hairColorMax = tonumber(v.hairColor)
            vo.fashionIDs = table.tonumber(string.split(v.equipres, "|"))

            if v.backGround then
                vo.bg = string.format("art/creatbg/%s.png", v.backGround)
            end
            if v.foreGround then
                vo.fg = string.format("art/creatbg/%s.png", v.foreGround)
            end

            ret[classID] = ret[classID] or {}
            ret[classID][raceID] = ret[classID][raceID] or {}
            ret[classID][raceID][gender] = vo
        end

        -- dump(ret, "GameDB:getCreateDatas", 10)
        self._createDatas = ret
        assert(next(ret) ~= nil, "GameDB:getCreateDatas fail")
    end

    return self._createDatas
end

-- 角色创建信息
function GameDB:getCreateData(classId, raceId, gender)
    if not self._createDatas then
        self:getCreateDatas()
    end
    return self._createDatas[classId][raceId][gender]
end

-- 获取死亡声音
function GameDB:getPlayerDeathSound( classID, raceID, gender )
	if tonumber( classID ) and tonumber( raceID ) and tonumber( gender ) then
        local conf = self:getCachedValue("create_total",function(v)
            return v.classID==tostring(classID) and v.raceID==tostring(raceID) and v.gender==tostring(gender)
        end)
		if conf then -- 有内容的表
            local soundFile = conf.deathSound
            if not soundFile then
                return nil
            end
            local path = "sfx/"..soundFile
            return path
		else
			return nil
		end
	else
		return nil
	end
end

function GameDB:getPropConfig(id)
    return self:getCachedValue("prop", id)
end


function GameDB:getPropConfigByName(name)
    return self:getCachedValue("prop", function(v)
        return name == v.name
    end)
end


function GameDB:getPropConfigs()
    return self:getCachedValues("prop")
end

function GameDB:getPropNameById(id)
    local conf = self:getCachedValue("prop", id)
    if conf then
        return conf.name
    else
        return id
    end
end

-- 1整数
-- 2万分之一 显示%
-- 3千分之一 显示小数
-- 4十分之一 显示小数
-- 5除以100
function GameDB:getPropValueShow(value, format)
    if format == 1 then

    elseif format == 2 then
        value = value / 100 .. "%"
    elseif format == 3 then
        value = value / 1000
    elseif format == 4 then
        value = value / 10
    elseif format == 5 then
        value = value / 100
    end

    return value
end

-- return name, value, icon
function GameDB:getPropShowData(id, value)
    local conf = self:getCachedValue("prop", id)
    if conf then
        value = self:getPropValueShow(value, tonumber(conf.format))

        -- 属性小图标
        local icon = conf.iconSmall
        if icon then
            icon = Res.tipsPropIconPath .. icon .. ".png"
        end
        return L(conf.name), value, icon or ""
    else
        return id, value, ""
    end
end

-- getPropShowData 多添加一个处理冒号问题
function GameDB:getPropShowDataAndHandlerColon(id, value)
    local name, value , icon= self:getPropShowData(id, value)
    name = string.gsub(name or "", "：+","") or ""
    name = string.gsub(name or "", ":+","") or ""
    name = name .. ": "
    return L(name), value, icon
end

function GameDB:getAvatarShopConf(classId, gender)
    local conf = self:getCachedValue("barber_shop", function(v)
        return tonumber(v.classID) == classId and tonumber(v.gender) == gender
    end)
    return conf
end

function GameDB:getAvatarGenderConf(gender)
    local conf = self:getCachedValue("barber_gender", gender)
    return conf
end

function GameDB:getHairColorConf(gender)
    local conf = self:getCachedValue("haircolor_res", gender)
    return conf
end

return GameDB
